import http from '@/shopro/request/index';
import share from '@/shopro/share';

const state = {
  shop: {}, // 商城信息
  wechat: {}, // 微信配置
  share: {}, // 分享配置
  payment: {}, // 支付配置
  addons: [], // 插件配置
  chat: uni.getStorageSync('chat') || {}, // 客服配置
  store: {}, // 商城信息
  tabbarData: [], //自定义底部导航数据
  recharge: uni.getStorageSync('recharge') || {}, //充值配置

  homeTemplate: [], // 首页模板数据
  userTemplate: [], // 个人中心模板数据
  floatData: {}, // 悬浮按钮数据
  popupData: {}, // 弹窗数据
  hasTemplate: true, //是否有模板数据
  shareInfo: {}, // 默认分享数据
  //分享状态
  shareStatus: {
    now: 0,
    status: '',
  },
  // 全局 跳过动画
  allSkip: uni.getStorageSync('skip') || false,
  // 全局 bgm开关
  bgm: uni.getStorageSync('open_bgm') || false,
  // 全局 音效开关
  sound: uni.getStorageSync('open_sound') || false,
};
const getters = {
  initShop: (state) => state.shop,
  initStore: (state) => state.store,
  initShare: (state) => state.share,
  initPayment: (state) => state.payment,
  initAddons: (state) => state.addons,
  initChat: (state) => state.chat,
  initWechat: (state) => state.wechat,
  initRecharge: (state) => state.recharge,

  hasTemplate: (state) => state.hasTemplate,
  homeTemplate: (state) => state.homeTemplate,
  userTemplate: (state) => state.userTemplate,
  floatData: (state) => state.floatData,
  popupData: (state) => state.popupData,
  tabbarData: (state) => state.tabbarData,

  shareInfo: (state) => state.shareInfo,
  allSkip: (state) => state.allSkip,
  bgm: (state) => state.bgm,
  sound: (state) => state.sound,
  shareStatus: (state) => state.shareStatus,
};

const actions = {
  // 初始化数据
  async appInit({ commit, dispatch }, options) {
    const result = await http('common.init');
    if (result.code === 1) {
      commit('CONFIG', result.data);
      if (!options?.query?.token) {
        dispatch('autoLogin');
      }
      return result.data;
    }
    return false;
  },

  // 获取模板数据
  async getTemplate({ commit }, options) {
    let shop_id = 0;
    // #ifdef H5
    if (options?.query.shop_id) {
      shop_id = options.query.shop_id;
    }
    // #endif

    // #ifdef MP
    if (options?.query.scene) {
      let scene = decodeURIComponent(options?.query.scene);
      let sceneObj = scene.split('=');
      if (sceneObj[0] === 'shop_id') {
        shop_id = sceneObj[1];
      }
    }
    // #endif
    // const result = await http(
    //   'common.template',
    //   shop_id
    //     ? {
    //         shop_id,
    //       }
    //     : {}
    // );
    // if (result.code === 1) {
    //   commit('hasTemplate', true);
    //   commit('TEMPLATE', result.data);
    //   return result.data;
    // } else {
    //   commit('hasTemplate', false);
    //   return false;
    // }
  },

  // 同步路由到后端
  syncPages({ commit }) {
    http('common.syncPages', {
      data: ROUTES,
    });
  },
  // 更改 跳过动画
  changeAllSkip({ commit }, status = false) {
    commit('CHANGE_All_SKIP', status);
  },
  // 更改BGM开关
  changeBgm({ commit }, status = false) {
    commit('CHANGE_BGM', status);
  },
  // 更改 音效开关
  changeSound({ commit }, status = false) {
    commit('CHANGE_SOUND', status);
  },
  // 全部操作 音效 bgm
  allSwitchVoice({ commit }, status = false) {
    commit('CHANGE_SOUND', status);
    commit('CHANGE_BGM', status);
  },
  //监听分享状态
  onShareStatus({ commit }, data = {}) {
    commit('ON_SHARE', data);
  },
};

const mutations = {
  CONFIG(state, payload) {
    Object.keys(payload).forEach((k) => {
      state[k] = payload[k];
      if (k === 'chat') {
        uni.setStorageSync('chat', payload[k]);
      }
      if (k === 'recharge') {
        uni.setStorageSync('recharge', payload[k]);
      }
    });
  },

  TEMPLATE(state, data) {
    state.template = data;
    state.homeTemplate = data.home;
    state.userTemplate = data.user;
    state.floatData = data['float-button']?.[0]?.content;
    state.popupData = data?.popup?.[0]?.content;
    state.tabbarData = data?.tabbar?.[0]?.content;
  },

  hasTemplate(state, data) {
    state.hasTemplate = data;
  },
  // 弹窗一次的话，关闭的时候删除数据。
  delPopup(state, index) {
    let popupData = state.popupData;
    popupData.list.splice(index, 1);
    state.template = popupData;
  },
  shareInfo(state, shareInfo) {
    state.shareInfo = shareInfo;
  },
  CHANGE_All_SKIP(state, status) {
    state.allSkip = status;
    uni.setStorageSync('skip', status);
  },
  CHANGE_SOUND(state, status) {
    state.sound = status;
    uni.setStorageSync('open_sound', status);
  },
  CHANGE_BGM(state, status) {
    state.bgm = status;
    uni.setStorageSync('open_bgm', status);
  },
  ON_SHARE(state, data) {
    if (data.status && data.now) {
      switch (data.status) {
        case 'share':
          state.shareStatus.status = data.status;
          state.shareStatus.now = data.now;
          break;
        case 'shared':
          if (state.shareStatus.status == 'share') {
            let time = data.now - state.shareStatus.now;
            if (time > 3000) {
              http('v1.getShareSuccess', {}).then((res) => {
                const type = {
                  show: '显示卡',
                  hint: '提示卡',
                };
                if (res.code === 1) {
                  if (res.data.res === 1) {
                    uni.showModal({
                      title: '每日任务-分享',
                      content: `分享成功,您成功获取${type[res.data.type]}x${
                        res.data.num
                      }`,
                      showCancel: false,
                      confirmText: '确定',
                    });
                  }
                }
              });
            }
            state.shareStatus = {
              status: '',
              now: 0,
            };
          }
          break;
      }
    }
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
